import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.sql.rowset.WebRowSet;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import javax.swing.JComboBox;

public class SubjectUI extends JFrame implements ActionListener{
	private JTextField txtMaMH;
	private JTextField txtTenMH;
	private JTextField txtSDVHT;
	private JComboBox cboMaLop;
	DefaultComboBoxModel cboModel;
	
	JTable table;
	DefaultTableModel model;
	
	JButton btnNew,btnSave,btnReset,btnDelete,btnExit,btnUpdate;
	
	WebRowSet rs=null;
	DataProcess data=null;
	private JLabel lblMaLop;

	public SubjectUI() {
		getContentPane().setLayout(null);
		
		try {
			try {
				data=(DataProcess)Naming.lookup("//khangutc/dkh");
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
		JLabel lblDanhSachMon = new JLabel("Danh Sach Mon Hoc");
		lblDanhSachMon.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDanhSachMon.setForeground(Color.RED);
		lblDanhSachMon.setBounds(280, 11, 143, 26);
		getContentPane().add(lblDanhSachMon);
		
		JLabel lblMaMh = new JLabel("Ma MH");
		lblMaMh.setBounds(31, 55, 54, 20);
		getContentPane().add(lblMaMh);
		
		txtMaMH = new JTextField();
		txtMaMH.setBounds(119, 55, 158, 20);
		getContentPane().add(txtMaMH);
		txtMaMH.setColumns(10);
		
		JLabel lblTenMh = new JLabel("Ten MH");
		lblTenMh.setBounds(31, 106, 54, 20);
		getContentPane().add(lblTenMh);
		
		txtTenMH = new JTextField();
		txtTenMH.setBounds(119, 106, 158, 20);
		getContentPane().add(txtTenMH);
		txtTenMH.setColumns(10);
		
		JLabel lblSoDvht = new JLabel("So DVHT");
		lblSoDvht.setBounds(310, 55, 68, 20);
		getContentPane().add(lblSoDvht);
		
		txtSDVHT = new JTextField();
		txtSDVHT.setBounds(401, 55, 158, 20);
		getContentPane().add(txtSDVHT);
		txtSDVHT.setColumns(10);
		
		/**
		 * Table Settings
		 */
		String[] header={"Ma MH","Ten MH","So DVHT","Ma Lop"};
		model= new DefaultTableModel(header,0);
		
		table= new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String id=String.valueOf(table.getValueAt(table.getSelectedRow(), 0));
				String name=String.valueOf(table.getValueAt(table.getSelectedRow(), 1));
				String DVHT=String.valueOf(table.getValueAt(table.getSelectedRow(), 2));
				String MaLop=String.valueOf(table.getValueAt(table.getSelectedRow(),3));
				txtMaMH.setText(id);
				txtTenMH.setText(name);
				txtSDVHT.setText(DVHT);
				cboMaLop.setSelectedItem(MaLop);
				btnDelete.setEnabled(true);
				btnUpdate.setEnabled(true);
				btnReset.setEnabled(true);
			}
		});
		fillTable();
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10, 173, 584, 141);
		getContentPane().add(scrollPane);
		
		btnNew = new JButton("New");
		btnNew.setBounds(10, 336, 89, 23);
		getContentPane().add(btnNew);
		
		btnUpdate = new JButton("Update");
		btnUpdate.setBounds(211, 336, 89, 23);
		getContentPane().add(btnUpdate);
		
		btnDelete = new JButton("Delete");
		btnDelete.setBounds(310, 336, 89, 23);
		getContentPane().add(btnDelete);
		
		btnReset = new JButton("Reset");
		btnReset.setBounds(406, 336, 89, 23);
		getContentPane().add(btnReset);
		
		btnExit = new JButton("Exit");
		btnExit.setBounds(505, 336, 89, 23);
		getContentPane().add(btnExit);
		
		btnSave = new JButton("Save");
		btnSave.setBounds(109, 336, 89, 23);
		getContentPane().add(btnSave);
		
		lblMaLop = new JLabel("Ma Lop");
		lblMaLop.setBounds(303, 106, 75, 20);
		getContentPane().add(lblMaLop);
		
		/**
		 * ComboBox Settings
		 */
		cboModel=new DefaultComboBoxModel();
		cboMaLop = new JComboBox(cboModel);
		cboMaLop.setBounds(401, 106, 158, 20);
		fillCombo();
		
		getContentPane().add(cboMaLop);
		
		JButton[] btn={btnNew,btnSave,btnReset,btnDelete,btnExit,btnUpdate};
		int i;
		for(i=0;i<btn.length;i++){
			btn[i].addActionListener(this);
		}
	}
	
	public static void main(String[] args){
		new SubjectUI().setVisible(true);
	}

	/**
	 * Fill ComboBox Method
	 */
	protected void fillCombo(){
		String sql="Class";
		try {
			rs=data.GetTable(sql);
			while(rs.next()){
				cboModel.addElement(rs.getString(1));
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Fill Table Method
	 */
	protected void fillTable(){
		try {
			rs= data.GetTable("Subject");
			while(rs.next()){
				String[] row={rs.getString(1),rs.getString(2),String.valueOf(rs.getInt(3)),rs.getString(4)};
				model.addRow(row);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
	}
	
	/**
	 * Update table khi them moi thanh cong
	 */
	protected void updateRow(){
		String condition="SubjectID='" + txtMaMH.getText() + "'";
			try {
				rs=data.GetRecord(condition,"Subject");
				while(rs.next()){
					String[] row={rs.getString(1),rs.getString(2),String.valueOf(rs.getInt(3)),rs.getString(4)};
					model.addRow(row);			
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
	}
	
	/**
	 * Cap nhat lai cho table khi update thanh cong
	 */
	protected void updateValue() {
		String Ten=txtTenMH.getText();
		String SDVHT=txtSDVHT.getText();
		String MaLop=cboMaLop.getSelectedItem().toString();
		
		table.setValueAt(Ten,table.getSelectedRow(),1);	
		table.setValueAt(SDVHT,table.getSelectedRow(),2);
		table.setValueAt(MaLop,table.getSelectedRow(),3);
	}
	
	/**
	 * Cap nhat lai cho table khi delete thanh cong
	 */
	protected void updateValuedelete() {
		model.removeRow(table.getSelectedRow());		
	}
	
	/**
	 * Xu ly them moi du lieu
	 */
	protected boolean insertData(String id, String ten, int DVHT,String MaLop) {
		String sql="insert into Subject values('" + id + "','" + ten + "'," + DVHT + ",'" + MaLop + "')";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Xu ly delete 1 ban ghi
	 * @param id
	 * @return
	 */
	protected boolean deleteData(String id){
		String sql="delete from Subject where SubjectID='" + id + "'";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Xu ly update du lieu
	 */
	protected boolean updateDate(String id, String name, int SDVHT,String MaLop){
		String sql="update Subject set SubjectName='" + name + "',UnitOfStudy=" + SDVHT + ",ClassID='" + MaLop + "' where SubjectID='" + id + "'";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Ham reset values
	 */
	protected void resetValues(){
		txtMaMH.setText("");
		txtTenMH.setText("");
		txtSDVHT.setText("");
		cboMaLop.setSelectedIndex(-1);
		txtMaMH.setEnabled(false);
		btnNew.setEnabled(true);
		btnDelete.setEnabled(false);
		btnSave.setEnabled(false);
		btnReset.setEnabled(false);
		btnUpdate.setEnabled(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnNew){
			resetValues();
			txtMaMH.setEnabled(true);
			txtMaMH.requestFocus();
			btnSave.setEnabled(true);
			btnReset.setEnabled(true);
		}
		
		if(e.getSource()==btnSave){
			String id=txtMaMH.getText();
			String name=txtTenMH.getText();
			int khoa = Integer.parseInt(txtSDVHT.getText());
			String MaLop=cboMaLop.getSelectedItem().toString();
			if(insertData(id, name,khoa,MaLop)){
				JOptionPane.showMessageDialog(null,"Save successed!");
				updateRow();
			}else{
				JOptionPane.showMessageDialog(null,"Insert failed!");
			}
		}
		
		if(e.getSource()==btnReset){
			resetValues();
		}
		
		if(e.getSource()==btnDelete){
			String id=txtMaMH.getText();
			if(deleteData(id)){
				JOptionPane.showMessageDialog(null,"Deleted!");
				updateValuedelete();
			}
			else{
				JOptionPane.showMessageDialog(null,"Delete failed!");
			}
			resetValues();
		}
		
		if(e.getSource()==btnUpdate){
			
			String id=txtMaMH.getText();
			String name=txtTenMH.getText();
			int Khoa= Integer.parseInt(txtSDVHT.getText());			
			String MaLop=cboMaLop.getSelectedItem().toString();
			
			if(updateDate(id, name,Khoa,MaLop)){
				JOptionPane.showMessageDialog(null,"Updated!");
				updateValue();
			}
			else{
				JOptionPane.showMessageDialog(null,"Update failed!");
			}
			resetValues();
		}
		
		if(e.getSource()==btnExit){
			System.exit(0);
		}
		
		
	}
}
